package com.javakc.pms.orderManagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.pms.instruction.entity.Instruction;
import com.javakc.pms.orderManagement.client.MesClient;
import com.javakc.pms.orderManagement.entiy.OrderManagement;
import com.javakc.pms.orderManagement.service.OrderManagementService;
import com.javakc.servicebase.config.InstructionQuery;
import com.javakc.servicebase.handler.HctfException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags="查询调度指令库管理")
@RestController
@RequestMapping("pms/ordermanagement")
public class OrderManagementController {

    @Autowired
    private OrderManagementService orderManagementService;

    @Autowired
    private MesClient mesClient;

    @ApiOperation(value = "查询所有指令库")
    @GetMapping
    public R findAll(){
        List<OrderManagement> list = orderManagementService.list();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询，查询调度指令库管理")
    @PostMapping("{pageNo}/{pageSize}")
    public R findPageOrderManagemente(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Long pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Long pageSize,
            @ApiParam(name = "instructionQuery", value = "查询条件封装对象", required = false)
            @RequestBody(required = false) InstructionQuery instructionQuery){
        Page<OrderManagement> orderManagementPage  = new Page<>(pageNo, pageSize);
        orderManagementService.findPageOrderManagement(orderManagementPage,instructionQuery);
        long total = orderManagementPage.getTotal();
        List<OrderManagement> orderManagementList = orderManagementPage.getRecords();
        return R.OK().data("total", total).data("items", orderManagementList);

    }

    @ApiOperation(value = "新增查询调度指令管理")
    @PostMapping("saveOrderManagement")
    public R saveOrderManagement(@RequestBody OrderManagement orderManagement){
        boolean flag = orderManagementService.save(orderManagement);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据Id单条数据查询")
    @GetMapping("getOrderManagement/{orderManagementId}")
    public R getOrderManagementById(@PathVariable("orderManagementId") String orderManagementId){
        OrderManagement orderManagement = orderManagementService.getById(orderManagementId);
        return R.OK().data("orderManagement",orderManagement);
    }

//    @ApiOperation(value = "根据ID修改查询调度指令库")
//    @PutMapping("updateOrderManagement")
//    public R updateOrderManagement(@RequestBody OrderManagement orderManagement){
//        OrderManagement orderManagements = orderManagementService.selectById(orderManagement.getId());
//        //设置下达状态与下达时间
//        orderManagements.setIsInstructionState(1);
//        orderManagements.setGiveTime(new Date());
//
//        //TODO下达
//        mesClient.saveRelease(orderManagements);
//        R r = mesClient.saveRelease(orderManagements);
//        if(r.getCode() ==20001){
//            throw  new HctfException(20001,r.getMessage());
//        }
//        //修改
//        boolean flag = orderManagementService.updateById(orderManagements);
//        if (flag)
//            return R.OK();
//        else
//            return R.ERROR();
//    }

    @ApiOperation(value = "修改调度指令")
    @PutMapping("updateorderManagement")
    public R updateOrderManagements(@RequestBody OrderManagement orderManagement){
        boolean flag = orderManagementService.updateById(orderManagement);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据ID删除查询调度指令库管理")
    @DeleteMapping("deleteOrderManagement/{orderManagementId}")
    public R deleteOrderManagementById(@PathVariable("orderManagementId")String orderManagementId){
        boolean flag = orderManagementService.removeById(orderManagementId);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "查询所有指令库1")
    @GetMapping("select")
    public R select(){
        List<OrderManagement> list = orderManagementService.select();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "指令状态修改与指令下达")
    @PutMapping("updateRelease/{orderManagementId}")
    public R updateRelease(@PathVariable("orderManagementId") int orderManagementId){
        OrderManagement orderManagement = orderManagementService.getById(orderManagementId);
        //设置下达状态与下达时间
        orderManagement.setIsInstructionState(1);
        orderManagement.setGiveTime(new Date());

        //TODO下达
        mesClient.saveRelease(orderManagement);
        //修改
        boolean flag = orderManagementService.updateById(orderManagement);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }


}
